<template>
  <div>
    <vue-particles
      id="tsparticles"
      :particlesInit="particlesInit"
      :particlesLoaded="particlesLoaded"
      :options="option"
    />

    <div class="loginFormBox">
      <h3>哈皮门户管理系统</h3>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        status-icon
        :rules="rules"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="loginForm.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { loadFull } from 'tsparticles'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'

const store = useStore()

const loginForm = reactive({
  userName: '',
  password: ''
}) // 表单绑定响应式对象

const loginFormRef = ref() // 表单引用对象

const rules = reactive({
  userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

const router = useRouter()
// 提交表单
const submitForm = () => {
  // 1. 表单校验
  loginFormRef.value.validate((valid) => {
    if (valid) {
      // console.log('校验数据', loginForm)
      axios.post('/adminApi/user/login', loginForm).then((res) => {
        // console.log(res.data.data)
        if (res.data.code === '0000') {
          // axios.get('/adminApi/user/menu').then(res => {
          //   store.commit('changeMenuList', res.data.data)
          // })
          store.commit('changeMenuList', res.data.data.roleMenu)
          store.commit('changeUserInfo', res.data.data)
          store.commit('changeGetterRouter', false)
          router.push('/home')
        } else {
          ElMessage.error(res.data.msg)
        }
      })
    }
  })
  // 2. 发送请求
  // 3. 拿到token跳转到首页
}

//  const handleLogin = () => {
//   localStorage.setItem('token', '123456')
// }

// 配置particles
const option = {
  background: {
    color: {
      value: '#0d47a1'
    }
  },
  fpsLimit: 120,
  interactivity: {
    events: {
      onClick: {
        enable: true,
        mode: 'push'
      },
      onHover: {
        enable: true,
        mode: 'repulse'
      },
      resize: true
    },
    modes: {
      bubble: {
        distance: 400,
        duration: 2,
        opacity: 0.8,
        size: 40
      },
      push: {
        quantity: 4
      },
      repulse: {
        distance: 200,
        duration: 0.4
      }
    }
  },
  particles: {
    color: {
      value: '#ffffff'
    },
    links: {
      color: '#ffffff',
      distance: 150,
      enable: true,
      opacity: 0.5,
      width: 1
    },
    collisions: {
      enable: true
    },
    move: {
      direction: 'none',
      enable: true,
      outModes: {
        default: 'bounce'
      },
      random: false,
      speed: 6,
      straight: false
    },
    number: {
      density: {
        enable: true,
        area: 800
      },
      value: 80
    },
    opacity: {
      value: 0.5
    },
    shape: {
      type: 'circle'
    },
    size: {
      value: { min: 1, max: 5 }
    }
  },
  detectRetina: true
}

const particlesInit = async (engine) => {
  await loadFull(engine)
}
</script>

<style lang="scss" scoped>
.loginFormBox {
  width: 400px;
  height: 260px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffffaf;
  text-align: center;
  padding: 20px;
  display: flex;
  justify-content: start;
  flex-direction: column;
  align-items: center;
  border-radius: 7px;
  .demo-ruleForm {
    width: 350px;
  }

  h3 {
    font-size: 20px;
    margin-bottom: 20px;
  }
}
::v-deep .el-form-item__label {
  color: #000000ee;
}
</style>
